var gulp = require("gulp");
var gulp_min_css = require("gulp-minify-css");
var gulp_scss = require("gulp-sass");
var gulp_plumber = require("gulp-plumber");
var gulp_tinymin = require("gulp-tinypng");
var gulp_uglify = require("gulp-uglify");
var gulp_copy = require("gulp-copy")

//定义任务
gulp.task("mincss",function(){
	//这个任务要做的事情就在这里写
	gulp.src('./scss/*.scss')//source
		.pipe(gulp_plumber())
		.pipe(gulp_scss())
		.pipe(gulp_min_css())
		.pipe(gulp.dest('./src/css'))
})

gulp.task("pngmin",function(){
	gulp.src('./**/*.*')
	.pipe(gulp.dest('./dist/img'))
})

//实时监听scss文件,并转换
gulp.task("watchscss",function(){
	gulp.watch("./scss/*.scss",["mincss"])
})


gulp.task("minimg",function(){
	//这个任务要做的事情就在这里写
	gulp.src('./img/*.*')//source
		.pipe(gulp_plumber())
		.pipe(gulp_tinymin("y4yWdMW4TZxmmWZnksSV_rd-Nl2AYSQw"))
		.pipe(gulp.dest('./dist/img'))
})
//npm i gulp-uglify -D

gulp.task("minjs",function(){
	gulp.src("js/**/*.js")
	.pipe(gulp_uglify())
	.pipegulp.dest("dist/js")
})

//定义自动刷新浏览器任务
var browser_sync = require("browser-sync").create();
gulp.task('browser_sync', function() {
    browser_sync.init({
        server: {
            baseDir: "./"
        }
    });
    gulp.watch("scss/**/*.scss",["mincss"]).on("change",browser_sync.reload);
    gulp.watch(".html").on("change",browser_sync.reload);
});


gulp.task('copy', function() {
    gulp.src("./src/css/**/*").pipe(gulp_copy("dist/css",{prefix:"2"}));//复制所有CSS文件
    gulp.src("./src/font/**/*").pipe(gulp_copy("dist/font",{prefix:"2"}));
    gulp.src("./src/**/*").pipe(gulp_copy("dist",{prefix:"1"}));
    gulp.src("./src/scss/**/*").pipe(gulp_copy("dist/scss",{prefix:"2"}));
});

//定义一键开发模式
gulp.task("dev",["browser-sync"]);

//定义一键生成模式
gulp.task("pro",["mincss","minjs","minimg"]);
